class Promise {
    constructor(executor) {
        this.PromiseState = 'pendding'
        this.PromiseResult = null
        const _this = this
        this.callbacks = []

        function resolve(data) {
            if (_this.PromiseState !== 'pendding') return
            _this.PromiseState = 'fulfilled'
            _this.PromiseResult = data

            // 执行器执行异步任务时,then方法内的回调需要在promise状态改变后再执行
            _this.callbacks.forEach(item => {
                setTimeout(() => {
                    item.onResolved(data)
                }, 1000)
            });

        }

        function reject(data) {
            if (_this.PromiseState !== 'pendding') return
            _this.PromiseState = 'rejected'
            _this.PromiseResult = data

            // 异步任务then的回调需要在promise状态改变后再执行
            _this.callbacks.forEach(item => {
                setTimeout(() => {
                    item.onRejected(data)
                })
            });
        }

        try {
            executor(resolve, reject)
        } catch (error) {
            reject(error)
        }
    }

    then(onResolved, onRejected) {
        const _this = this

        if (typeof onRejected !== 'function') {
            onRejected = reason => {
                throw reason
            }
        }

        if (typeof onResolved !== 'function') {
            onResolved = value => value
        }

        return new Promise((resolve, reject) => {
            function callback(type) {
                try {
                    let result = type(_this.PromiseResult)
                    if (result instanceof Promise) {
                        result.then(v => {
                            resolve(v)
                        }, r => {
                            reject(r)
                        })
                    } else {
                        resolve(result)
                    }
                } catch (error) {
                    reject(error)
                }
            }

            // 判断fulfilled状态下
            if (this.PromiseState === 'fulfilled') {
                setTimeout(() => {
                    callback(onResolved)
                })
            }

            // 判断rejected状态下
            if (this.PromiseState === 'rejected') {
                setTimeout(() => {
                    callback(onRejected)
                })
            }

            // 判断pendding状态下
            if (this.PromiseState === 'pendding') {
                this.callbacks.push({
                    onResolved: function () {
                        callback(onResolved)
                    },
                    onRejected: function () {
                        callback(onRejected)
                    },
                })
            }
        })
    }

    catch(onRejected) {
        return this.then(undefined, onRejected)
    }

    static resolve(value) {
        return new Promise((resolve, reject) => {
            if (value instanceof Promise) {
                value.then(v => {
                    resolve(value)
                }, r => {
                    reject(r)
                })
            } else {
                resolve(value)
            }
        })
    }
    static reject(reason) {
        return new Promise((resolve, reject) => {
            reject(reason)
        })
    }
    static all(promises) {
        return new Promise((resolve, reject) => {
            let count = 0
            let arr = []
            for (let i = 0; i < promises.length; i++) {
                promises[i].then(v => {
                    count++
                    arr[i] = v
                    if (count === promises.length) {
                        resolve(arr)
                    }
                }, r => {
                    reject(r)
                })
            }
        })

    }
    // 那个promise参数先进来就把它的状态赋值给return出去的promise (赛跑api)
    static race(promises) {
        return new Promise((resolve, reject) => {
            for (let i = 0; i < promises.length; i++) {
                promises[i].then(v => {
                    resolve(v)
                }, r => {
                    reject(r)
                })
            }
        })

    }
}

// then的promiseState状态:在new Promise下,取决于new Promise的promiseState
// then的promiseResult值:在new Promise下取决于new Promise修改状态时所携带的参数
// 若new Promise的then方法中成功回调执行,则表示new Promise的PromiseState状态为resolve
// 若new Promise的then方法中失败回调执行,则表示new Promise的PromiseState状态为reject
// 终上所述:在new Promise的then方法回调中,可以确认new Promise的promiseState状态了(定死了,promiseState的状态不可逆)则可以安全的将外部then的promiseState修改为同样状态,并拿到new Promise修改状态时携带的参数value





